package cn.easybuy.servlet.backend;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;

import cn.easybuy.entity.ProductCategory;
import cn.easybuy.service.ProductCategoryService;
import cn.easybuy.service.impl.ProductCategoryServiceImpl;
import cn.easybuy.util.PageUtil;
import cn.easybuy.util.ReturnResult;

public class AdminCategoryServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	private ProductCategoryService productCategoryService = new ProductCategoryServiceImpl();
       
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		String action = request.getParameter("action");
		if (action != null && action.length()>0) {
			switch (action) {
			case "index":
				index(request,response);
				break;
			case "toAddProductCategory":
				toAddProductCategory(request,response);
				break;
			case "queryProductCategoryList":
				queryProductCategoryList(request,response);
				break;
			case "addProductCategory":
				addProductCategory(request,response);
				break;
			case "toUpdateProductCategory":
				toUpdateProductCategory(request,response);
				break;
			case "modifyProductCategory":
				modifyProductCategory(request,response);
				break;
			default:
				response.sendRedirect("404.jsp");
				break;
			}
		} else {
			response.sendRedirect("404.jsp");
		}
	}

	private void modifyProductCategory(HttpServletRequest request, HttpServletResponse response) throws IOException {
		PrintWriter out = response.getWriter();
		
        Integer parentId=0;
        //获取分类级别
        String type=request.getParameter("type");
        String productCategoryLevel1=request.getParameter("productCategoryLevel1");
        String productCategoryLevel2=request.getParameter("productCategoryLevel2");
        String name=request.getParameter("name");
        String id=request.getParameter("id");
        if(type.equals("1")){
            parentId =0;
        }else if(type.equals("2")){
            parentId =Integer.parseInt(productCategoryLevel1);
        }else{
            parentId =Integer.parseInt(productCategoryLevel2);
        }
        ProductCategory productCategory =new ProductCategory();
        productCategory.setId(Integer.parseInt(id));
        productCategory.setName(name);
        productCategory.setParentId(parentId);
        productCategory.setType(Integer.parseInt(type));
        productCategory.setIconClass("");
        int result = productCategoryService.update(productCategory);
        
        ReturnResult returnResult = new ReturnResult(0,"修改分类失败");
		
        if (result > 0) {
        	returnResult.setStatus(1);
        	returnResult.setMessage("修改分类成功");
		}
		
		String jsonStr = JSON.toJSONString(returnResult);
		
		out.print(jsonStr);
		
		out.flush();
		out.close();
		
	}

	private void toUpdateProductCategory(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String id=request.getParameter("id");
        ProductCategory productCategory=productCategoryService.findById(Integer.parseInt(id));
        List<ProductCategory> productCategoryList1=null;
        List<ProductCategory> productCategoryList2=null;
        request.setAttribute("productCategory",productCategory);
        //判断分类级别
        if(productCategory.getType()==2){
            productCategoryList1=productCategoryService.findListByType(1);
        }
        if(productCategory.getType()==3){
        	//拿到所有的一级分类
        	productCategoryList1=productCategoryService.findListByType(1);
        	
        	//二级分类要先确定productCategory对应的一级分类
        	//拿到当前分类的父级分类
        	ProductCategory parentCategory = productCategoryService.findById(productCategory.getParentId());
        	//拿到二级分类
            productCategoryList2=productCategoryService.findAllByParentId(parentCategory.getParentId());
            
            request.setAttribute("parentCategory",parentCategory);
        }
        request.setAttribute("productCategoryList1",productCategoryList1);
        request.setAttribute("productCategoryList2",productCategoryList2);


        request.getRequestDispatcher("/backend/productCategory/toAddProductCategory2.jsp").forward(request, response);
		
	}

	private void addProductCategory(HttpServletRequest request, HttpServletResponse response) throws IOException {
		PrintWriter out = response.getWriter();
		
        Integer parentId=0;
        //获取分类级别
        String type=request.getParameter("type");
        String productCategoryLevel1=request.getParameter("productCategoryLevel1");
        String productCategoryLevel2=request.getParameter("productCategoryLevel2");
        String name=request.getParameter("name");
        if(type.equals("1")){
            parentId =0;
        }else if(type.equals("2")){
            parentId =Integer.parseInt(productCategoryLevel1);
        }else{
            parentId =Integer.parseInt(productCategoryLevel2);
        }
        ProductCategory productCategory =new ProductCategory();
        productCategory.setName(name);
        productCategory.setParentId(parentId);
        productCategory.setType(Integer.parseInt(type));
        productCategory.setIconClass("");
        int result = productCategoryService.add(productCategory);
        
        ReturnResult returnResult = new ReturnResult(0,"新增分类失败");
		
        if (result > 0) {
        	returnResult.setStatus(1);
        	returnResult.setMessage("新增分类成功");
		}
		
		String jsonStr = JSON.toJSONString(returnResult);
		
		out.print(jsonStr);
		
		out.flush();
		out.close();
		
	}

	private void queryProductCategoryList(HttpServletRequest request, HttpServletResponse response) throws IOException {


		String parentId=request.getParameter("parentId");
       
        
        List<ProductCategory> productCategoryList=productCategoryService.findAllByParentId(Integer.parseInt(parentId));
        
        PrintWriter out = response.getWriter();
        
        String jsonStr = JSON.toJSONString(productCategoryList);
		
		out.print(jsonStr);
		
		out.flush();
		out.close();
	}

	private void toAddProductCategory(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.getRequestDispatcher("/backend/productCategory/toAddProductCategory2.jsp").forward(request, response);
	}

	private void index(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		int currPageNo = 1;
		int pageSize = 8;
		
		String paramCurrPageNo = request.getParameter("currPageNo");
		if (paramCurrPageNo != null && !"".equals(paramCurrPageNo)) {
			currPageNo = Integer.parseInt(paramCurrPageNo);
		}
		
		PageUtil<ProductCategory> pageUtil = productCategoryService.findAllProductCategory(currPageNo, pageSize);
		
		pageUtil.setCurrPageNo(currPageNo);
		//listProduct
		request.setAttribute("pageUtil", pageUtil);
		request.getRequestDispatcher("/backend/productCategory/productCategoryList.jsp").forward(request, response);
		
		
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
